<template>
  <div>
    <vxe-toolbar id="dictId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      show-footer
      highlight-hover-row
      row-id="id"
      :edit-config="{trigger: 'click', mode: 'row', showStatus: true}"
      class="mytable-footer"
      height="700"
      align="center"
      :data="tableData"
      :export-config='{type:"csv"}'
      remote-sort
      @sort-change="sortList"
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" width="60"></vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="code" title="编码" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.code" placeholder="请输入编码" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="name" title="名称" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.name" placeholder="请输入名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="status" title="状态" min-width="100" :formatter="formatterStatus" sortable />
        <template v-slot:header="{ column }">

          <el-select v-model="listQuery.status" clearable placeholder="请选择">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="remark" title="备注" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.remark" placeholder="请输入备注" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="sort" title="排序" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.sort" placeholder="请输入排序" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" oninput="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
           type='number' clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right">
        <template slot-scope="scope">
          <el-tooltip effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.id)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.id)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="500" height="520" destroy-on-close  resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel" :operationType="operationType"/>
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="500" height="520" destroy-on-close  resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" :operationId="operationId" />
    </vxe-modal>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import compAdd from './components/dictAdd.vue'
import compEdit from './components/dictEdit.vue'
import compDetail from './components/dictDetail.vue'
import request from '@/utils/request'
export default {
  name: 'DictList',
  components: {
    compAdd,
    compEdit,
    compDetail
  },
  data() {
    return {
      message: '',
      loading: false,
      tableData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      modelDetailFlag: false,
      copy: false,
      listQuery: {
        id: '',
        type: '',
        code: '',
        name: '',
        status: '',
        remark: '',
        sort: '',
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      },
      // 状态
      statusList: [
        { label: '正常', value: 0 },
        { label: '停用', value: 1 }
      ],
      operationId:0,  // 编辑组件接口所需id
      operationType:'', // 新增组件接口所需id
    }
  },
  created() {

  },
  mounted() {
    this.getDictType()
    this.findList()
  },
  methods: {
    // 获取数据字典类型
    getDictType() {
      const path = this.$route.path
      var dictType = path.split('/dict/')[1]
      if (dictType == undefined) {
        dictType = path.split('/wmsChargingType/')[1]
      }
      this.listQuery.type = dictType
      // 设置子组件字典类型
      this.operationType = dictType
    },
    findList() {
      this.loading = true
      return new Promise(resolve => {
        request({
          url: '/arpa-basic-api/dict',
          method: 'get',
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records
          this.listQuery.total = response.data.total
          this.loading = false
          resolve();
        })
      })
    },
    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 翻译状态
    formatterStatus({ cellValue }) {
      const item = this.statusList.find(item => item.value === cellValue)
      return item ? item.label : ''
    },
    // 添加方法
    funcAdd() {
      this.modelAddFlag = true
    },
    // 修改方法
    funcEdit(id) {
      this.operationId = id
      this.modelEditFlag = true
    },
    // 详情方法
    funcDetail(id) {
      this.$refs.detailRef.getInfo(id)
      this.modelDetailFlag = true
    },
    // 删除方法
    funcDelete(id) {
      this.$confirm('确认删除吗？').then(_ => {
        request({ url: `/arpa-basic-api/dict`, method: 'delete', params: { ids: id }}).then(response => {
          this.$message({ message: response.msg, type: 'success' })
          this.findList()
        })
      }).catch(_ => {})
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break
        case 'edit':
          this.modelEditFlag = false
          break
        case 'detail':
          this.modelDetailFlag = false
          break
        default:
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },
  }
}
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis), .vxe-table .vxe-footer--column:not(.col--ellipsis), .vxe-table .vxe-header--column:not(.col--ellipsis) {
 padding: 6px 0;
}
.vxe-toolbar{padding:0 0 10px 0}
</style>

